/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.appstate;

import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.shape.Box;
import com.jme3.util.BufferUtils;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import model.DiCeng_Model;
import model.GongZuoMian_Model;
import myjme.tin.core.GridController;

/**
 *
 * @author Administrator
 */
public class DiCengAppState extends AbstractAppState{
    private float gzmcdF=350;
    private float gzmzxcdF=1000;
    private SimpleApplication app;
    private Camera cam;
    private Node rootNode;
    private AssetManager assetManager;
    private static final int DiCeng_Type_NiYanI=10;
    private static final int DiCeng_Type_ShaYanI=11;
    private boolean drawflag=true;
    private MyController controller;
    private MyBrain brain;
    private Node diCengContainerNode=new Node();
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app          = (SimpleApplication) app;
        this.cam          = this.app.getCamera();
        this.rootNode     = this.app.getRootNode();
        this.assetManager = this.app.getAssetManager();  
        rootNode.attachChild(diCengContainerNode);
        
    }

    @Override
    public void cleanup() {
        super.cleanup();
        while(diCengContainerNode.getChildren().size()>0){
            diCengContainerNode.getChildren().remove(diCengContainerNode.getChildren().size()-1);
        }
        //diCengContainerNode.rotate(-controller.getMEICENGQINGJIAOF()* FastMath.DEG_TO_RAD, 0, 0);
    }
    public void setDiCengSize() throws Exception{
        ArrayList<GongZuoMian_Model> gzmlistArrayList=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
        if(gzmlistArrayList.size()>0){
            GongZuoMian_Model gzm=gzmlistArrayList.get(0);
            gzmcdF=Float.parseFloat(gzm.getGzmcd());
            gzmzxcdF=Float.parseFloat(gzm.getGzmzxcdString());
        }else{
            drawflag=false;
        }
        
        diCengContainerNode.setLocalRotation(new Quaternion().fromAngleAxis(controller.getMEICENGQINGJIAOF()*FastMath.DEG_TO_RAD, Vector3f.UNIT_X));
    }
    
    public void drawDiCeng_LingYiGe(){
        if(drawflag){
            Box shangFuYanCeng=new Box(new Vector3f(gzmcdF/2.0f,2.5f,-gzmzxcdF/2.0f), gzmcdF/2.0f+2f, 1.5f, gzmzxcdF/2.0f+1);
            Geometry shangGeometry=new Geometry(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SHANG"), shangFuYanCeng);
            Material material=new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            shangGeometry.setMaterial(material);
            material.setColor("Color",new ColorRGBA(118.0f/255.0f,77.0f/255.0f,57.0f/255.0f, 0.7f));
            diCengContainerNode.attachChild(shangGeometry);

            
            ArrayList<DiCeng_Model> dcList=OperateMyDatabaseImpl.doGetDiCengAll();
//            for(int i=0;i<dcList.size();i++){
//                DiCeng_Model dcm=dcList.get(i);
//                String mc=dcm.getMc();
//                String jl=dcm.getJl();
//                float jlF= Float.valueOf(jl);
//                String hd=dcm.getHd();
//                float hdF=Float.valueOf(hd);
//                Box aoHuiCengBox=new Box(gzmcdF, hdF/2.0f, gzmzxcdF);
//                Geometry diCengGeometry=new Geometry(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("AOHUI"), aoHuiCengBox);
//                Material aoHuiMaterial=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
//                aoHuiMaterial.setColor("Color", ColorRGBA.randomColor());
//                diCengGeometry.setMaterial(aoHuiMaterial);
//                diCengGeometry.move(new Vector3f(gzmcdF/2.0f,-jlF-4.0f-hdF/2.0f,-gzmzxcdF/2.0f));
//                diCengContainerNode.attachChild(diCengGeometry);
//            }
            //新建的网格。
            GridController gc=new GridController(450, 1500, 200, 200);
            gc.doGrid();
            Vector3f v[]=gc.getVal();
            int[] indexs=gc.getIndices();
            float[] colors=gc.getColorArray();
            //创建一个mesh
            Mesh m=new Mesh();
            m.setBuffer(VertexBuffer.Type.Position,3, BufferUtils.createFloatBuffer(v));
            m.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indexs));
            m.setBuffer(VertexBuffer.Type.Color, 4, colors);
            m.setMode(Mesh.Mode.Lines);
            m.updateBound();
            Geometry geo = new Geometry("OurMesh", m); // using our custom mesh object
            Material mat1 = new Material(assetManager, 
                "Common/MatDefs/Misc/Unshaded.j3md");
            //mat1.setColor("Color", ColorRGBA.Green);
            mat1.setBoolean("VertexColor", true);
            geo.setMaterial(mat1);
            geo.setLocalTranslation(-100, -60, 200);
            diCengContainerNode.attachChild(geo);
            
            
            Box shangFuYanCeng1=new Box(new Vector3f(gzmcdF/2.0f,8.5f,-gzmzxcdF/2.0f), gzmcdF/2.0f+2f, 4.5f, gzmzxcdF/2.0f+1);
            Geometry shangGeometry1=new Geometry(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SHANG1"), shangFuYanCeng1);
            Material material1=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
            material1.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), new ColorRGBA(161.0f/255.0f,23.0f/255.0f,21.0f/255.0f, 0.7f));
            shangGeometry1.setMaterial(material1);
            diCengContainerNode.attachChild(shangGeometry1);
        }else{
            while(diCengContainerNode.getChildren().size()>0){
                diCengContainerNode.getChildren().remove(diCengContainerNode.getChildren().size()-1);
            }
        }
        drawflag=true;
        
    }
    public Spatial getABox(int Di_Ceng_Type,float xI,float yI,float zI){

        if(Di_Ceng_Type==DiCeng_Type_NiYanI){
            Spatial spatial=assetManager.loadModel(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MODELS/NIYAN2/NIYAN.J3O"));
            spatial.move(xI, yI, zI*9);
            return spatial;
        }else{
            Spatial spatial=assetManager.loadModel(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("MODELS/FENSHAYAN9/FENSHAYAN.J3O"));
            spatial.move(xI, yI, zI*9);
            return spatial;
        }
        
    }
    
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void addController(MyController controller){
        this.controller=controller;
    }
}
